# -*- coding: utf-8 -*-
# created on 2016/12/20

from sympy import Symbol

from mathsolver.functions.base import BaseFunction, new_latex, BasePoly, BaseVariable
from mathsolver.functions.hanshu.chudeng_hanshu_jisuan import ChuDengHanShuJiSuan
from mathsolver.functions.hanshu.dingyiyu import QiuDingYiYu
from mathsolver.functions.hanshu.helper import check_func


class IsJieXiShiEq(BaseFunction):
    """判断函数解析式是否相同"""

    def solver(self, *args):
        func1, func2 = check_func(args[0]), check_func(args[1])
        expr1, expr2 = func1.expression, func2.expression
        jisuan_step = ChuDengHanShuJiSuan().solver(BasePoly(expr1 - expr2))
        self.steps.extend(jisuan_step.steps)
        res = jisuan_step.output[0].value == 0
        if res:
            self.steps.append(["", "所以函数 %s 与 %s 的解析式相同" % (func1.printing(), func2.printing())])
        else:
            self.steps.append(["", "所以函数 %s 与 %s 的解析式不同" % (func1.printing(), func2.printing())])

        self.output.append(BaseVariable(res))
        self.label.add("判断函数解析式是否相同")
        return self


class HanShuXiangTong(BaseFunction):
    """判断函数是否为同一函数"""

    def solver(self, *args):
        # var 标准化，统一使用 x
        func1, func2 = args
        x = Symbol('x')
        expr1 = func1.expression.subs(func1.var, x)
        expr2 = func2.expression.subs(func2.var, x)
        func1.var = x
        func2.var = x
        func1.expression = expr1
        func2.expression = expr2

        # 求定义域
        dingyiyu1 = QiuDingYiYu().solver(func1)
        dingyiyu2 = QiuDingYiYu().solver(func2)

        self.steps.append(["%s 的定义域为 %s" % (func1.printing(), new_latex(dingyiyu1)),
                           "%s 的定义域为 %s" % (func2.printing(), new_latex(dingyiyu2))])

        # 判断定义域是否相等
        if dingyiyu1 != dingyiyu2:
            self.steps.append(["", "所以函数的定义域不相等，所以不是同一个函数"])
            res = False
        else:
            self.steps.append(["", "所以函数的定义域相等"])

            # 判断函数解析式是否相同
            jiexishi_step = IsJieXiShiEq().solver(func1, func2)
            self.steps.extend(jiexishi_step.steps)
            jiexishi_eq = jiexishi_step.output[0].value

            if jiexishi_eq:
                self.steps.append(["", "两个函数的定义域和解析式都相同，所以它们是同一个函数"])
            else:
                self.steps.append(["", "所以两个函数不是同一个函数"])
            res = jiexishi_eq

        self.label.add("判断函数是否为同一函数")
        self.output.append(BaseVariable(res))
        return self


if __name__ == '__main__':
    pass
